#!/usr/bin/env python
#-*- coding: UTF-8 -*- 

# python3 
import apache_log_parser
import sys
import os
import urllib.parse
import pymysql
import time
from datetime import datetime
import argparse

recommenduid_key='recommenduid'
recommenduid_pv_data={}
# 常量用大写定义
# status_ok = '200'
STATUS_OK = '200'

operation_time=None
file_recommenduid=None
current_time=None
write_file=None

line_parser = apache_log_parser.make_parser("%h \"%r\" %>s %b")

db_name = 'rebate_db'
config = {
    'host': '39.100.119.163',
    'port':3306,
    'user': 'emei',
    'password': 'emei123',
    'charset': 'utf8mb4',
    'db':db_name
}
table_name = 'rebate_user_pv'
conn = pymysql.connect(**config)
cursor = conn.cursor()




# 读取文件内容
def read_file(filename):
  fopen_obj = open(filename, 'r')
  for each_line in fopen_obj:
    parser_line_data = line_parser(each_line)
    # 去除http状态不是200
    if parser_line_data['status'] != STATUS_OK :
       continue
    # 获取URL 
    url = parser_line_data['request_url']
    # 获取URL的参数
    query = dict(urllib.parse.parse_qsl(urllib.parse.urlsplit(url).query))
    # 去除参数中不含recommenduid_key变量值
    if recommenduid_key not in query :
       continue
    # print("-数据组装 --> {'11814': 5, '11816': 1}-")
    if query[recommenduid_key] not in recommenduid_pv_data :
        recommenduid_pv_data[query[recommenduid_key]]=0
    recommenduid_pv_data[query[recommenduid_key]] += 1
  fopen_obj.close()


# pv 数据写入数据库
def insert_pv_data(data):
    insert_list = []
    data_length = len(data)
    i = 0
    for uid in data:
        sql="insert into rebate_db.rebate_user_pv (uid,pv,status,create_time,update_time,operation_time) values(%s,%s,%s,%s,%s,%s)"
        insert_list.append((uid,data[uid],1,current_time,current_time,operation_time))
        i +=1
        if i%6 == 0 or i == data_length:
            # 写入sql 语句记录日志
            pymysql_executemany(sql,insert_list)
            insert_list = []


def close_conn():
    cursor.close()
    conn.close()

# 执行sql语句 并记录日志
def pymysql_executemany(sql,data):
    cursor.executemany(sql,data)
    sql_execute=cursor._executed.decode('utf-8')
    conn.commit()
    # sql 语句写入到文件
    fopen = open(write_file, 'a')
    fopen.write('%s%s' % (sql_execute,"\n"))
    fopen.close()


def main():
    try:
        # 读取文件内容并设置数据
        read_file(file_recommenduid)
        print(recommenduid_pv_data)
        # pv 数据写入数据库
        insert_pv_data(recommenduid_pv_data)
        close_conn()
    except ValueError:
        close_conn()


if __name__ == '__main__':
    # 获取参数 
    parser = argparse.ArgumentParser(description='manual to this script')
    # file_recommenduid 初始化日志文件
    parser.add_argument("--file_recommenduid", type=str, default="")
    # operation_time 哪一天的数据记录时间
    parser.add_argument("--operation_time", type=str, default="")
    # current_time 当前时间
    parser.add_argument("--current_time", type=str, default="")
    # write_file 需要记录结果的日志
    parser.add_argument("--write_file", type=str, default="")

    args = parser.parse_args()
    # print(args)
    file_recommenduid = args.file_recommenduid
    operation_time=args.operation_time
    current_time=args.current_time
    write_file=args.write_file
    main()











